
from django import http
from django.core.paginator import Paginator, EmptyPage
from django.core.paginator import Paginator
from django.http import HttpResponseNotFound
from django.shortcuts import render
from django.views import View

from apps.goods.models import FoodsCategory, FoodsChannel
from apps.goods.utils import get_categories, get_breadcrumb


class FoodsListView(View):
    def get(self,request):
        """提供商品列表页"""
        c_id = request.GET.get('c_id')
        print(c_id)
        #判断category_id是否正确
        try:
            category = FoodsCategory.objects.filter(group_id=c_id)
            print(category)
        except FoodsCategory.DoesNotExist:
            return HttpResponseNotFound('FoodsCategory does not exist')

        categories = get_categories() #调用utils里的显示分类方法

        # 渲染页面
        context = {
            'category':category,
            'categories':categories,
        }

        return render(request,'menu.html',context)

class FoodProductsView(View):
    def get(self,request):
        p_id = request.GET.get('p_id')
        # page_num = request.GET.get('page_num')
        print(p_id,11111)
        # print(page_num)
        try:
            # product_cate1 = FoodsCategory.objects.get(id=p_id)
            product_cate = FoodsChannel.objects.filter(category_id=p_id)
            # print(product_cate.id)
            # product_cate_id = FoodsSKU.objects.filter(p)
            # print(product_cate_id)

        except Exception as e:
            # print(e)
            # return HttpResponseNotFound('Foodschannel does not exist')
            return HttpResponseNotFound(e)

        # hot_foods = FoodsChannel.objects.filter()
        # 创建分页器：每页N条记录
        # paginator = Paginator(product_cate,constants.Foods_LIST_LIMIT)
        # #获取每页商品数据
        # try:
        #     page_foods = paginator.page(p_id)
        # except EmptyPage as e:
        #     print(e)
        # #     #如果page_num不正确，默认给用户404
        #     return http.HttpResponseNotFound('empty page')
        # # 获取列表页总页数
        # total_page = paginator.num_pages

        categories = get_categories()  # 调用显示分类方法
        # 渲染模板的上下文
        context = {
            'product_cate':product_cate,
            'categories':categories,
            # 'product_cate1':product_cate1,
            # 'page_foods':page_foods,
            # 'page_num':p_id,
            # 'total_page':total_page,
        }

        return render(request,'products.html',context)

